<?php
/**
 * Dispute (argument) data mapper class.
 *
 * @category Zend
 * @package Model
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 */

class Disputeo_Model_DisputeMapper
{
    const DISPUTE_TOP = 'top';
    const DISPUTE_NEW = 'new';
    const DISPUTE_OPEN = 'open';
    
    /**
     * Get top disputes
     * 
     * @param int $categoryId
     * @param int $page
     * @return Zend_Paginator
     */
    public static function getTop($categoryId = null, $page = 1)
    {
        return self::_getPaginator(self::DISPUTE_TOP, $categoryId, $page);
    }
    
    /**
     * Get new disputes
     * 
     * @param int $categoryId
     * @param int $page
     * @return Zend_Paginator
     */
    public static function getNew($categoryId = null, $page = 1)
    {
        return self::_getPaginator(self::DISPUTE_NEW, $categoryId, $page);
    }
    
    /**
     * Get open disputes
     * 
     * @param int $categoryId
     * @param int $page
     * @return Zend_Paginator
     */
    public static function getOpen($categoryId = null, $page = 1)
    {
        return self::_getPaginator(self::DISPUTE_OPEN, $categoryId, $page);
    }
    
    /**
     * Get a paginator for the disputes
     * 
     * @param string $type
     * @param int $categoryId
     * @return Zend_Paginator
     */
    protected static function _getPaginator($type, $categoryId = null, $page = 1)
    {
        $dispute = Application_Model_Dispute::create();
        $select = $dispute->select()
            ->from('dispute', 
                array('disputeId', 'userId', 'title', 'argument', 'created', 'score'))
            ->join('category', 'dispute.categoryId = category.categoryId',
                array('category'))
            ->join(array('u' => 'user'), 'dispute.userId = u.userId', 
                array('creator' => 'userName'));
        
        if ($categoryId) {
            $select->where('category.categoryId = ?', $categoryId);
        }
        
        switch ($type) {
            case self::DISPUTE_NEW:
                $select->order('created DESC');
                break;
            case self::DISPUTE_OPEN:
                $select->where('dispute.posts = 0')
                    ->order('created DESC');
                break;
            default:
                $select->where('dispute.posts > 0')
                    ->order('created DESC');
        }
        
        $adapter = new Disputeo_Paginator_Adapter($select);
        $adapter->setModel(new Application_Model_Dispute);
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($page);
        
        return $paginator;
    }
    
    /**
     * Returns an array of Application_Model_Response objects
     * 
     * @param type $disputeId
     * @return array
     */
    public static function fetchResponses($disputeId)
    {
        $table = new Application_Model_DbTable_Response();
        return $table->fetchAll(
            $table->getAdapter()->quoteInto('disputeId = ?', $disputeId)
        );
    }
}